import { Router } from "express";
import { AuthMiddleware } from "../../../core/middleware/auth.js";
import { AuthController } from "./controller.js";
import { errorHandler } from './../../../core/middleware/errorHandler.js';
import { loginSchema, refreshTokenSchema,registerSchema,captchaSchema } from "./schema.js";
import { API_URLS } from "../../../config/api-urls.js";

const router = Router();

// 或者如果只想测试路由注册：
router.get("/login/test", (req, res) => res.send("Test route works"));

// 管理员登录
router.post(
  API_URLS.PASSWORD_LOGIN,
  AuthMiddleware.validate(loginSchema,{skipAuth:true}),
  AuthController.login
);

// 验证token (需要认证)
router.get(
  API_URLS.VERIFY_TOKEN,
  AuthMiddleware.verifyToken(),
  AuthController.getAuthenticatedUser
);
// 刷新token
router.post(
  API_URLS.REFRESH_TOKEN,
  AuthMiddleware.validate(refreshTokenSchema,{skipAuth:true}),
  AuthController.refreshToken
);

// 退出登录 (需要认证)
router.post(
  API_URLS.LOGOUT,
  AuthMiddleware.authenticate(process.env.JWT_SECRET),
  AuthController.logout
);

// 用户注册（无需认证）
router.post(
  API_URLS.REGISTER,
  AuthMiddleware.validate(registerSchema, { skipAuth: true }),
  AuthController.registers
);

// 发送注册验证码（无需认证）
router.post(
  API_URLS.REGISTER_CAPTCHA,
  AuthMiddleware.validate(captchaSchema, { skipAuth: true }),
  AuthController.sendRegisterCaptcha
);

// 验证邮箱（需要认证）
router.post(
  API_URLS.VERIFY_EMAIL,
  AuthMiddleware.verifyToken(),
  AuthController.verifyEmail
);
// 在所有路由之后添加错误处理中间件
router.use(errorHandler);

export default router;
